from django import forms
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _


def username_val(comment):
    try:
        User.objects.get(username=comment)
        raise ValidationError(
            '用户名已存在'
            # params={'value': '用户名必须为手机号'}
        )
    except:
        if len(comment) != 11:
            raise ValidationError(
                '用户名必须为手机号'
                # params={'value': '用户名必须为手机号'}
            )


def password_val(comment):
    if len(comment) <= 6:
        raise ValidationError('密码必须大于６位')


class RegisterForm(forms.Form):
    username = forms.CharField(
        label='用户名',
        validators=[username_val, ],
    )
    name = forms.CharField(label='姓名')
    password = forms.CharField(
        label='密码',
        validators=[password_val, ],
    )
    password_rep = forms.CharField(label='确认密码')
